package com.wangxiang.sys.manage.service.Impl;

import com.wangxiang.sys.manage.dao.RoleMenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.Query;

import com.wangxiang.sys.manage.dao.MenuDao;
import com.wangxiang.sys.manage.entity.MenuEntity;
import com.wangxiang.sys.manage.service.MenuService;


@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleMenuDao roleMenuDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
      return null;
    }

    /**
     * 菜单获取
     * @return
     */
    @Override
    public List<MenuEntity> getMenu(int roleId) {
        //通过权限id获取菜单id
        List<Integer> menuIdList = roleMenuDao.getMenuIdList(roleId);
        ArrayList<MenuEntity> menulist = new ArrayList<>();
        menuIdList.forEach((idItem)->{
            //查询该角色的所有菜单
            MenuEntity menuEntity = menuDao.selectById(idItem);
            menulist.add(menuEntity);
        });

        //组合
        ArrayList<MenuEntity> list = new ArrayList<>();
        //一级分类
        ArrayList<MenuEntity> onelist = new ArrayList<>();
        //二级分类
        ArrayList<MenuEntity> twolist = new ArrayList<>();

        menulist.forEach((menuItem)->{
            //找到所有的一级分类
            if (menuItem.getLevel()==0){
                onelist.add(menuItem);
            }
            //找到二级分类
            if (menuItem.getLevel()==1){
                twolist.add(menuItem);
            }
        });
        //组合
        onelist.forEach((oneItem)->{
            MenuEntity menu = oneItem;
            ArrayList<MenuEntity> two = new ArrayList<>();
            twolist.forEach((twoItem)->{
                if (oneItem.getId()==twoItem.getParentId()){
                    two.add(twoItem);
                }
            });
            menu.setChildren(two);
            list.add(menu);
//            System.out.println(list);

        });

//        System.out.println(list);
        return list;
    }

}